<?php

namespace GanYiInfo;

use GanYiInfo\Http\RequestBodyJsonEncoded;

class Request
{
    protected $headers = [];

    protected $params = [];

    protected $method;

    protected $endpoint;

    protected $baseUrl;

    public function __construct($method = null, $endpoint = null, array $params = [])
    {
        $this->setMethod($method);
        $this->setParams($params);
        $this->setEndpoint($endpoint);
    }

    public function getMethod()
    {
        return $this->method;
    }

    public function setMethod($method)
    {
        $this->method = strtoupper($method);

        return $this;
    }

    public function getHeaders()
    {
        return $this->headers;
    }

    public function setHeaders(array $headers)
    {
        $this->headers = $headers;

        return $this;
    }

    public function getParams()
    {
        return $this->params;
    }

    public function setParams(array $params)
    {
        $this->params = $params;

        return $this;
    }

    public function getEndpoint()
    {
        return $this->endpoint;
    }

    public function setEndpoint($endpoint)
    {
        $this->endpoint = $endpoint;

        return $this;
    }

    public function setBaseUrl($baseUrl)
    {
        $this->baseUrl = $baseUrl;

        return $this;
    }
    /**
     * Only return params on POST requests.
     *
     * @return array
     */
    public function getPostParams()
    {
        if ($this->getMethod() === 'POST') {
            return $this->getParams();
        }

        return [];
    }

    /**
     * Returns the body of the request as URL-encoded.
     *
     * @return RequestBodyJsonEncoded
     */
    public function getUrlEncodedBody()
    {
        $params = $this->getPostParams();

        return new RequestBodyJsonEncoded($params);
    }

    public function getUrl()
    {
        return $this->baseUrl . $this->endpoint;
    }
}